class ListNode {

  constructor(val) {
    this.val = val,
      this.next = null
  }

  create(arr) {
    if (!Array.isArray(arr) || !arr.length) {
      return new ListNode(null)
    }
    let head = new ListNode(null)
    let cur = head
    for (let i = 0; i < arr.length; i++) {
      cur.next = new ListNode(arr[i])
      cur = cur.next
    }
    return head
  }

  display(link) {
    let arr = []
    let head = link
    let p = head
    while (p !== null) {
      arr.push(p.val)
      p = p.next
    }
    return arr.join('-->')
  }
}

function mergeLinkList(list1, list2) {
  let l1 = new ListNode(null)
  let l2 = new ListNode(null)

  let link1 = l1.create(list1)
  let link2 = l2.create(list2)

  let p1 = link1.next
  let p2 = link2.next
  let head = new ListNode(null)
  let p = head
  while (p1 !== null && p2 !== null) {
    if (p1.val > p2.val) {
      p.next = p2
      p2 = p2.next
    } else {
      p.next = p1
      p1 = p1.next
    }
    p = p.next
  }
  if (p1 !== null) {
    p.next = p1
  }
  if (p2 !== null) {
    p.next = p2
  }

  let a = new ListNode()

  return a.display(p.next)
}

console.log(mergeLinkList([1, 3, 5], [2, 4, 6]));